﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
namespace myomscl_web.Admin.UserControls
{
    public partial class ucAddNewListScreen : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadTree();
            }
        }

        public delegate void OnTreeClick(string type, string id);

        public event OnTreeClick TreeClick;

        protected void tvUserTypes_SelectedNodeChanged(object sender, EventArgs e)
        {
            var selectedNode = this.tvUserTypes.SelectedNode;
            if (selectedNode != null)
            {
                if (string.IsNullOrEmpty(selectedNode.Value))
                {
                    TreeClick(null, null);
                }
                else
                {
                    var parameters = selectedNode.Value.Split(new char[] { ',' });
                    TreeClick(parameters[0], parameters[1]);
                }
            }
        }

        public void LoadTree()
        {
            LoadTree(null, null);
        }

        public void LoadTree(string type, string value)
        {
            this.tvUserTypes.Nodes.Clear();
            using (OmsModelContainer context = new OmsModelContainer())
            {
                TreeNode root = new TreeNode("Пользователи");
                root.Value = string.Empty;
                if (type == null)
                    root.Selected = true;
                var userTypes = context.UserTypes.Include("Organisations").ToList();
                foreach (var ut in userTypes)
                {
                    var utNode = new TreeNode(ut.Name);
                    utNode.Value = string.Format("ut,{0}", ut.Id);
                    if (type != null && type == "ut" &&
                        value == ut.Name)
                        utNode.Selected = true;
                    root.ChildNodes.Add(utNode);
                    foreach (var org in ut.Organisations)
                    {
                        var orgNode = new TreeNode(org.Name);
                        orgNode.Value = string.Format("org,{0}", org.Id);
                        if (type != null && type == "org" &&
                        value == org.Name)
                            orgNode.Selected = true;
                        utNode.ChildNodes.Add(orgNode);
                        org.Users.Load();
                        foreach (var us in org.Users)
                        {
                            string name = string.Format("{0} {1}", us.FirstName, us.LastName);
                            var usNode = new TreeNode(name);
                            usNode.Value = string.Format("us,{0}", us.Id);
                            if (type != null && type == "us" &&
                        value == name)
                                usNode.Selected = true;
                            usNode.ChildNodes.Add(usNode);
                        }
                    }
                }

                tvUserTypes.Nodes.Add(root);
            }
        }

    }
}